/*
 * Index.java
 *
 * Created on 15 de abril de 2007, 18:10
 */

package servidor;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;

import java.util.*;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.NodeList;

/**
 *
 * @author Pellizard
 */
public class Index {
    
    private Vector chapter;
    
    public Index() {
        
        chapter = new Vector();
    }
    
    /**
     * Creates a new instance of Index
     */
    public Index( Vector temas ) {
        
        this.setChapter(temas);
    }
    
    public Node toXml(){
      try
      {
          DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
          DocumentBuilder parser = fact.newDocumentBuilder();
          Document doc = parser.newDocument();

          return toXml( doc );
      }
      catch( Exception ex )
      {
         return null;
      }
  }
    
    public Node toXml( Document doc ){
        
      try {
      Node root = doc.createElement("index");
      //doc.appendChild(root);

      for( int i = 0; i < getChapter().size(); i++ )
      {
          Chapter c = (Chapter)getChapter().get( i ); //MAL!
          root.appendChild( c.toXml(doc) );
      }
      
      return root;

    } catch (Exception ex) {
      System.err.println("+============================+");
      System.err.println("|        XML Error           |");
      System.err.println("+============================+");
      System.err.println(ex.getClass());
      System.err.println(ex.getMessage());
      System.err.println("+============================+");
      ex.printStackTrace();
      return null;
    }
  } 
    
      public static Index fromXml( String str )
  {
        Vector chapters = new Vector();
      
        ByteArrayInputStream is;
        try{
             is = new ByteArrayInputStream( str.getBytes("UTF8") );
        }catch(Exception e){
            return null;
        }
        
        try
        {
            DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
            DocumentBuilder parser = fact.newDocumentBuilder();
            Document doc = parser.parse( is );
            
            NodeList nodes = doc.getElementsByTagName( "chapter" );
            
            for( int i = 0 ; i < nodes.getLength() ; i++ )
            {
                Node node = nodes.item( i );
                Chapter chapter = Chapter.fromXml( node.getOwnerDocument() );
                
                if( chapter != null )
                    chapters.add( chapter );
            }
            
            return new Index( chapters );
        }
        catch( Exception ex )
        {
            ex.printStackTrace();
            return null;
        }
  }
      
      public static Index fromXml( Document doc )
  {
        Vector chapters = new Vector();
      
        try
        {
            NodeList nodes = doc.getElementsByTagName( "chapter" );
            
            for( int i = 0 ; i < nodes.getLength() ; i++ )
            {
                Node node = nodes.item( i );
                Chapter chapter = Chapter.fromXml( node.getOwnerDocument() );
                
                if( chapter != null )
                    chapters.add( chapter );
            }
            
            return new Index( chapters );
        }
        catch( Exception ex )
        {
            ex.printStackTrace();
            return null;
        }
  }


    public Vector getChapter() {
        return chapter;
    }

    public void setChapter(Vector chapter) {
        this.chapter = chapter;
    }
    
    public void addChapter( Chapter chapter )
    {
        this.chapter.add( chapter );
    }
    
    public String toString()
    {
        TransformerFactory transFactory = TransformerFactory.newInstance();
        try
        {
            Transformer transformer = transFactory.newTransformer();
            DOMSource source = new DOMSource(this.toXml());
            ByteArrayOutputStream s = new ByteArrayOutputStream();
            StreamResult result = new StreamResult(s);
            transformer.transform(source, result);
            return s.toString();
        }
        catch( Exception e ){  };
        return "";
    }
}
